package com.edu.buservice.dal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.buservice.dal.mapper.SysUserRolesMapper;
import com.edu.buservice.dal.model.SysUserRoles;
import com.edu.buservice.dal.service.SysUserRolesService;
import com.edu.buservice.util.model.enums.ErrorCodeEnum;
import com.edu.buservice.util.model.error.BizException;
import org.springframework.stereotype.Service;

/**
* @author 86177
* @description 针对表【sys_user_roles(用户角色表)】的数据库操作Service实现
* @createDate 2025-02-05 23:34:12
*/
@Service
public class SysUserRolesServiceImpl extends ServiceImpl<SysUserRolesMapper, SysUserRoles>
    implements SysUserRolesService{

	@Override
	public SysUserRoles getRolesByUserId(String userId) {

		if(StrUtil.isBlank(userId)){
			throw new BizException(ErrorCodeEnum.PARAM_MISSING, "userId不能为null");
		}

		LambdaQueryWrapper<SysUserRoles> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(SysUserRoles::getUserId, userId);
		wrapper.eq(SysUserRoles::getDeleted, 0);
		wrapper.eq(SysUserRoles::getStatus, 1);

		return getOne(wrapper);
	}

	@Override
	public void addUserRole(String userId, String roleId) {

		if(StrUtil.isBlank(userId) || StrUtil.isBlank(roleId)){
			throw new BizException(ErrorCodeEnum.PARAM_MISSING, "userId或roleId不能为null");
		}

		SysUserRoles sysUserRoles = new SysUserRoles();
		sysUserRoles.setUserId(userId);
		sysUserRoles.setRoleId(roleId);
		sysUserRoles.setCreatedBy(userId);
		save(sysUserRoles);

	}
}




